/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
// FREQUENCIAxDIRECAO
package br.leiam.eolica.graficos;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

import br.leiam.eolica.BD.Consulta;

/**
 * 
 * @author denis
 */
public class RosaDosVentosFrequenciaDirecao extends JPanel {
	private Consulta tableModel;
	private boolean haveData = false;

	private int xc = 0, yc = 0;// ponto x e y do centro

	public RosaDosVentosFrequenciaDirecao() {
		this.setSize(500, 500);
	}

	@Override
	protected void paintComponent(Graphics g1) {
		super.paintComponent(g1);
		Graphics2D g = (Graphics2D) g1;

		int recuo = 0;
		xc = this.getWidth() / 2;
		yc = this.getHeight() / 2;
		g.setColor(Color.white);

		;
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		g.setColor(Color.darkGray);
		float[] Dashes = { 10.0F, 5.0F, 5.0F, 5.0F };
		g.setStroke(new BasicStroke(1f, BasicStroke.CAP_SQUARE,
				BasicStroke.JOIN_MITER, 10F, Dashes, 0F));
		g.drawLine(this.getWidth() - 1, 1, this.getWidth() - 1, yc);
		g.setStroke(new BasicStroke(1f));
		// g.drawLine(xc, 1, this.getWidth()-1, 1);
		// parte dos 30%
		g.setColor(Color.white);
		g.fillArc(recuo, recuo, this.getWidth() - 2 * recuo - 1,
				this.getHeight() - 2 * recuo - 1, 0, 360);
		g.setColor(Color.darkGray);
		g.drawArc(recuo, recuo, this.getWidth() - 2 * recuo - 1,
				this.getHeight() - 2 * recuo - 1, 0, 360);
		// parte dos 20%
		int ix20 = xc - this.getWidth() / 3;
		int ixc20 = (int) (this.getWidth() / 1.5);
		int iy20 = yc - this.getHeight() / 3;
		int iyc20 = (int) (this.getHeight() / 1.5);
		g.drawArc(ix20, iy20, ixc20, iyc20, 0, 360);

		// parte dos 10%
		int ix10 = xc - this.getWidth() / 6;
		int ixc10 = this.getWidth() / 3;
		int iy10 = yc - this.getHeight() / 6;
		int iyc10 = this.getHeight() / 3;
		g.drawArc(ix10, iy10, ixc10, iyc10, 0, 360);

		// angulos
		// 0
		g.drawLine(recuo, yc, this.getWidth() - recuo, yc);
		// 90
		g.drawLine(xc, recuo, xc, this.getHeight() - recuo);
		double vlrMax = 0;
		if (haveData) {
			g.setColor(Color.blue);
			int i = 0, x = 75;

			for (i = 0; i < tableModel.getNumeroDeLinhas(); i++) {
				String valors = "0";
				double temp = 0;
				try {
					valors = tableModel.getValorEm(i, 1).toString();
				} catch (Exception e) {
					valors = "0";
				}

				temp = Double.valueOf(valors);
				if (temp >= vlrMax) {
					while (temp > vlrMax)
						vlrMax = vlrMax + 10;
				}
			}
			for (i = 0; i < tableModel.getNumeroDeLinhas(); i++) {
				double valorx = 0;
				double valory = 0;
				String valors = "0";
				double temp = 0;
				try {
					valors = tableModel.getValorEm(i, 1).toString();
				} catch (Exception e) {
					valors = "0";
				}

				// g.setColor(Color.getHSBColor((float)x-15, x-15, x-15));
				g.setColor(Color.green);
				temp = Double.valueOf(valors);
				// valorx=(temp*100/100)*(this.getWidth()/100);
				// valory=(temp*100/100)*(this.getHeight()/100);
				valorx = (temp / vlrMax) * (this.getWidth());
				valory = (temp / vlrMax) * (this.getHeight());

				// System.out.println(valory);
				g.fillArc(xc - (int) valorx / 2, yc - (int) valory / 2,
						(int) valorx, (int) valory, x + 2, -28);
				// g.fillArc(xc-(int)valorx/2, yc-(int)valory/2, (int) valorx,
				// (int) valory, x+2,28);
				// System.out.println(x);
				x = x - 30;
			}
		}

		g.setColor(Color.darkGray);
		g.setFont(g.getFont()
				.deriveFont((float) (this.getWidth()) * 14f / 300f));
		g.drawString(String.valueOf(vlrMax) + " %",
				this.getWidth() - 50 * this.getWidth() / 300, 12);
	}

	/**
	 * @return the tableModel
	 */
	public Consulta getTableModel() {
		return tableModel;
	}

	/**
	 * @param tableModel
	 *            the tableModel to set
	 */
	public void setTableModel(Consulta tableModel) {
		this.tableModel = tableModel;
		// System.out.println(this.tableModel.getRowCount());
		haveData = true;
		this.repaint();
	}

}
